define(['../../carrys/cityList','../../carrys/jsonList','../../../../js/lib/other/sweet-alert'],function(cityData,jsonList) {
    return ['$scope','$location', '$interval','$timeout','bizProxy', function($scope,$location,$interval,$timeout,bizProxy) {
		$scope.$emit("top-show",'true');//顶部显示
    	$scope.$emit('menuTypeLight','mine'); 	//控制底部menu高亮
    	$scope.$emit('bub-menu','true'); 	//隐藏底部menu
    	$scope.$emit('flag-tag','13-1');//显示底部文字
    	
    	$scope.isslide_up = false;//默认擅长领域
    	var caseTypeList = transData.storage_baseData.queryAdvFieldList.advFieldList;
        caseTypeList.forEach(function(e){
        	e.caseTypeName = e.advFieldName;
        	e.caseTypeId = e.advFieldId;
            e.sel = false;
        });
    	var goodFieldNum;
    	var caseTempTypeData = new Array();
    	caseTempTypeList(caseTypeList);
    	
    	var provinceList = new Array();
    	var cityListTemp = new Array();
    	var caseTypeTempList = new Array();
    	
    	
    	//根据userType默认是律师还是用户('1':律师  '0':用户)
    	if(transData.userType === '1'){
    		$scope.showLs = false,$scope.showPt = false;
    		$scope.isidt_card_no = true;
			$scope.lsSlt = true,$scope.ptSlt = false,$scope.isslide_down = true;
			var lsDataInfo = resultData.loginUserData;
			$scope.lawyerNo = lsDataInfo.lawyerNo;
			$scope.lawfirmName = lsDataInfo.lawfirmName;
			$scope.inviteCode = lsDataInfo.inviteCode;
			if(! lsDataInfo.advArea){
				$scope.seProvinceName = "请选择省份";
				$scope.seCityName = "请选择城市";
			}else{
				$scope.seProvinceName = (lsDataInfo.advArea).split('_')[0];
				$scope.seCityName = (lsDataInfo.advArea).split('_')[1];
			}
			
			try{
				if((lsDataInfo.advField).indexOf(',') != -1){
		    		var tempData = (lsDataInfo.advField).split(",");
		    		for(var i=0;i<tempData.length;i++){
		    			var val = tempData[i];
		    			var name = (caseTempTypeData[val]).caseTypeName;
		    			caseTypeTempList.push(name);
		    		}
		    		$scope.caseTypeData = caseTypeTempList;
		    	}else{
		    		caseTypeTempList = new Array();
		    		if(lsDataInfo.advField != "" && lsDataInfo.advField != null){
		    			var val = lsDataInfo.advField;
		    			caseTypeTempList[0] = (caseTempTypeData[val]).caseTypeName;
		        		$scope.caseTypeData = caseTypeTempList;
		    		}else{
		    			$scope.caseTypeData = "";
		    		}
		    	}
			}catch(e){
				caseTypeTempList = new Array();
	    		if(lsDataInfo.advField != "" && lsDataInfo.advField != null){
	    			var val = lsDataInfo.advField;
	    			caseTypeTempList[0] = (caseTempTypeData[val]).caseTypeName;
	        		$scope.caseTypeData = caseTypeTempList;
	    		}else{
	    			$scope.caseTypeData = "";
	    		}
			}
		}else if(transData.userType === '0'){
			$scope.showLs = true,$scope.showPt = true;
			$scope.isidt_card_no = false;
			$scope.lsSlt = false,$scope.ptSlt = true,$scope.isslide_down = false;
			$scope.lawyerNo = "";
			$scope.inviteCode = "";
			$scope.seProvinceName = "请选择省份";
			$scope.seCityName = "请选择城市";
			/*$scope.bankName = "";
			$scope.bankNo = "";
			$scope.branchName = "";
        	$scope.accountName = "";*/
			$scope.caseTypeData = "";
		}
    	
    	$scope.userName = resultData.loginUserData.userName,$scope.idType = resultData.loginUserData.idType,$scope.mobileNo = transData.mobileNo,
    	$scope.documentsNo = resultData.loginUserData.idNo,
    	$scope.email = resultData.loginUserData.email,
    	$scope.provinceName = "",$scope.cityName = "";
    	
    	collateData();//整理列表数据
    	//显示省份
    	$scope.provinceData = provinceList;
    	
    	//显示城市
    	$scope.clickCity = function(){
    		var cityList = new Array();
    		var provinceName = ($scope.provinceName).province;
    		for(var i=0;i<cityListTemp.length;i++){
    			if(cityListTemp[i].province == provinceName){
    				var tempData = {'city': cityListTemp[i].city};
    				cityList.push(tempData);
    			}
    		}
    		$scope.cityData = cityList;
    	}
    	
    	
    	//选中普通用户
    	$scope.clickPtFn = function(){
    		$scope.lsSlt = false,$scope.ptSlt = true,$scope.isslide_down = false;
    	}
    	
    	//选中律师用户
    	$scope.clickLsFn = function(){
    		$scope.lsSlt = true,$scope.ptSlt = false,$scope.isslide_down = true;
    	}
    	
    	//点击显示擅长领域
    	$scope.showField = function(){
    		$scope.isslide_up = true;
    		console.log('caseTypeTempList',caseTypeTempList)
    		console.log('caseTypeList',caseTypeList)
    		if(caseTypeTempList.length > 0){
    			for(var i=0;i<caseTypeTempList.length;i++){
    				for(var j=0;j<caseTypeList.length;j++){
    					if(caseTypeTempList[i] == caseTypeList[j].caseTypeName){
    						caseTypeList[j].sel = true;
    					}
    				}
    			}
    			$scope.caseTypeList = caseTypeList;
    		}else{
    			$scope.caseTypeList = caseTypeList;
    		}
    	}
    	
    	//点击关闭擅长领域
    	$scope.closeType = function(){
    		$scope.isslide_up = false;
    	}
    	
    	//选择案件类型
    	$scope.selectType = function(flag){
            var num=0;
            for(var i=0;i<caseTypeList.length;i++){
                if(caseTypeList[i].sel){
                    num++
                }
            }
            if(caseTypeList[flag].sel){
                caseTypeList[flag].sel = false;
            }else{
                if(num<4){
                    caseTypeList[flag].sel = true;
                }
            }
    	}
    	
    	//确认选择
    	$scope.confirmBtn = function(){
    		var tempFieldString = "";
    		var tempArray = new Array();
			for(var i=0;i<caseTypeList.length;i++){
				if(caseTypeList[i].sel){
					tempArray.push(caseTypeList[i].caseTypeName);
					tempFieldString = tempFieldString + caseTypeList[i].caseTypeId + ",";
				}
			}
    		$scope.isslide_up = false;
    		goodFieldNum = tempFieldString.substring(0,tempFieldString.length-1);
    		if(tempArray.length  < 1){
    			$scope.caseTypeData = "";
    		}else{
    			caseTypeTempList = tempArray;
    			$scope.caseTypeData = tempArray;
    		}
    	}
    	
    	//点击提交
        $scope.subConfirm = function(){
        	if(! $scope.userName){
        		$scope.$emit("promptAlertMsg","用户姓名不能为空");
        		$scope.$emit("showCollect",true);
        		$timeout(function(){
        			$scope.$emit("showCollect",false);
        		},transData.promptTime);
                return;
    		}else if(! $scope.documentsNo){
    			$scope.$emit("promptAlertMsg","证件号码不能为空");
        		$scope.$emit("showCollect",true);
        		$timeout(function(){
        			$scope.$emit("showCollect",false);
        		},transData.promptTime);
                return;
    		}else if(!_YHC_util.IdentityCodeValid($scope.documentsNo) && $scope.idType === '1'){
				$scope.$emit("promptAlertMsg","无效身份证号");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
			}else if( !_YHC_util.isValidBusCode($scope.documentsNo) && $scope.idType === '2'){
				$scope.$emit("promptAlertMsg","无效营业执照号");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
			}else if( !_YHC_util.isValidOrgCode($scope.documentsNo) && $scope.idType === '3'){
				$scope.$emit("promptAlertMsg","无效组织机构代码");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
			}else if(!setStatus($scope.email,'email') && $scope.email){
				$scope.$emit("promptAlertMsg","邮箱格式不正确");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
    			return;
			}else{
				if($scope.lsSlt){
    				if(! $scope.lawyerNo){
            			$scope.$emit("promptAlertMsg","律师证号不能为空");
                		$scope.$emit("showCollect",true);
                		$timeout(function(){
                			$scope.$emit("showCollect",false);
                		},transData.promptTime);
                        return;
            		}else if(!$scope.lawfirmName){
                        $scope.$emit("promptAlertMsg","律所名称不能为空");
                        $scope.$emit("showCollect",true);
                        $timeout(function(){
                            $scope.$emit("showCollect",false);
                        },transData.promptTime);
                        return;
                    }else if(($scope.lawyerNo).length != 17 || isNaN($scope.lawyerNo *1)){
            			$scope.$emit("promptAlertMsg","律师证号格式不正确");
                		$scope.$emit("showCollect",true);
                		$timeout(function(){
                			$scope.$emit("showCollect",false);
                		},transData.promptTime);
                        return;
            		}
    				subLsData();
    			}else{
    				subPtData();
    			}
			}
        }
        
        
        function setStatus(data,name){
        	var flag = false;
        	if(data != ""){
        		if(name == 'email'){
        			if(!_YHC_util.fChkMail(data)){
            			flag = false;
            		}else{
            			flag = true;
            		}
        		}
        	}else{
        		 flag = true;
        	}
        	
        	return flag;
        }
        
      //处理擅长领域
    	function caseTempTypeList(data){
    		for(var i=0;i<data.length; i++){
    			caseTempTypeData[data[i].caseTypeId] = data[i];
    		}
    	}
        
      //调用代理提交律师用户完善资料
        function subLsData(){
        	var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
        	
        	bizInfo.userName = $scope.userName;
        	bizInfo.idType = $scope.idType;
        	bizInfo.idNo = $scope.documentsNo;
        	bizInfo.email = $scope.email;
        	bizInfo.lawyerNo = $scope.lawyerNo;
        	bizInfo.lawfirmName = $scope.lawfirmName;
        	bizInfo.inviteCode = $scope.inviteCode;
        	if($scope.provinceName != "" && $scope.cityName != ""){
        		bizInfo.advArea = ($scope.provinceName).province + "_" + ($scope.cityName).city;
        	}else{
        		bizInfo.advArea = $scope.seProvinceName + "_" + $scope.seCityName;
        	}
        	
        	if(caseTypeTempList.length > 0){
        		bizInfo.advField = goodFieldNum;
        	}else{
        		bizInfo.advField = "";
        	}
        	
        	// bizInfo.bankName = "";
        	// bizInfo.accountNo = "";
        	// bizInfo.branchName = "";
        	// bizInfo.accountName = "";
    		$scope.$emit("loadingChange",true);
        	console.log(bizInfo)
    		bizProxy.queryProxyBizData("userExt4Lawyer",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
                        console.log(tempData)
		    			$timeout(function(){
		    				$scope.$emit("loadingChange",false);
		    				assignmentData(tempData);
		    				var tempBody = JSON.parse(tempData.body);
	    						$scope.$emit("goNext","/mineLs");
						},0);
		    		},function(data){
                    console.log(data)
		    			if(data != ""){//FAIL
		    				$scope.$emit("loadingChange",false);
							if(data.errorCode == ERROR_CODE.noLogin){
								FIREFLY("", data.errorMsg,"",function(){
										//调用退出登录
										transData.mobileNo = "";
		    							transData.password = "";
		    							transData.sessionId = "";
		    							transData.userType = "";
		    							resultData.loginUserData = "";
		    							$timeout(function(){
		    								$scope.$emit("goNext","/mine");
		    							},0);
								});
							}else{
								$scope.$emit("loadingChange",false);
								$scope.$emit("promptAlertMsg",data.errorMsg);
				    			$scope.$emit("showCollect",true);
				    			$timeout(function(){
				    				$scope.$emit("showCollect",false);
				    				if(transData.userType === '1'){
				    					$scope.$emit("goNext","/mineLs");
				    				}else if(transData.userType === '0'){
				    					$scope.$emit("goNext","/minePt");
				    				}
				    				
				    			},transData.promptTime);
							}
		    			}else{
		    				$scope.$emit("loadingChange",false);
							$scope.$emit("promptAlertMsg","连接超时");
			    			$scope.$emit("showCollect",true);
			    			$timeout(function(){
			    				$scope.$emit("showCollect",false);
			    				if(transData.userType === '1'){
			    					$scope.$emit("goNext","/mineLs");
			    				}else if(transData.userType === '0'){
			    					$scope.$emit("goNext","/minePt");
			    				}
			    			},transData.promptTime);
		    			}
		    		});
        }
        
        
      //调用代理提交普通用户完善资料
        function subPtData(){
        	var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
        	
        	bizInfo.userName = $scope.userName;
        	bizInfo.idType = resultData.loginUserData.idType;
        	
        	bizInfo.idNo = $scope.documentsNo;
        	bizInfo.email = $scope.email;

            console.log(bizInfo)
    		$scope.$emit("loadingChange",true);
    		bizProxy.queryProxyBizData("userExt",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
                        console.log(tempData)
    					$scope.$emit("loadingChange",false);
    					assignmentData(tempData);
						$timeout(function(){
							$scope.$emit("goNext","/minePt");
						},100);
		    		},function(data){
    					console.log(data)
		    			if(data != ""){//FAIL
		    				$scope.$emit("loadingChange",false);
							if(data.errorCode == ERROR_CODE.noLogin){
								FIREFLY("", data.errorMsg,"",function(){
										//调用退出登录
										transData.mobileNo = "";
		    							transData.password = "";
		    							transData.sessionId = "";
		    							transData.userType = "";
		    							resultData.loginResultData = "";
		    							resultData.loginUserData = "";
		    							$timeout(function(){
		    								$scope.$emit("goNext","/mine");
		    							},0);
								});
							}else{
								$scope.$emit("loadingChange",false);
								$scope.$emit("promptAlertMsg",data.errorMsg);
				    			$scope.$emit("showCollect",true);
				    			$timeout(function(){
				    				$scope.$emit("showCollect",false);
				    				if(transData.userType === '1'){
				    					$scope.$emit("goNext","/mineLs");
				    				}else if(transData.userType === '0'){
				    					$scope.$emit("goNext","/minePt");
				    				}
				    			},transData.promptTime);
							}
		    			}else{
		    				$scope.$emit("loadingChange",false);
							$scope.$emit("promptAlertMsg","连接超时");
			    			$scope.$emit("showCollect",true);
			    			$timeout(function(){
			    				$scope.$emit("showCollect",false);
			    				if(transData.userType === '1'){
			    					$scope.$emit("goNext","/mineLs");
			    				}else if(transData.userType === '0'){
			    					$scope.$emit("goNext","/minePt");
			    				}
			    			},transData.promptTime);
		    			}
		    		});
        }

    	//整理省市数据
    	function collateData() {
    		cityListTemp = new Array();
			for(var key in cityData.addressDetail){
				var tempData = {"province":key};
				provinceList.push(tempData);
				collateCity(key,cityData.addressDetail[key]);
			}
    	}
		function collateCity(data1,data2) {
			for(var key in data2){
				var tempList = {'province':data1,'city': key};
				cityListTemp.push(tempList);
			}
		}
        $scope.$apply();
    }];
});